<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    /* 
      https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof/
      思路：https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof/solution/er-fen-fa-javascriptshi-xian-by-xin-tan/
     */
    let nums = [0,1,3]; // output:2
    // let nums = [0,1,2,3,4,5,6,7,9]; // output:8

    function missingNumber(nums) {
      let left = 0, right = nums.length - 1;

      while (left <= right) {
        let mid = left + right >> 1;
        if (mid === nums[mid])
          left = mid + 1;
        else 
          right = mid - 1;
      }

      return left;
    }
    console.log(missingNumber(nums));
  </script>
</body>
</html>